﻿using Sundry.BLL;
using Sundry.IBLL;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;

namespace Sundry.App_Start.Common
{
    public class UserCookie
    {
        #region 管理员
        public const string adminCookieName = "sundryAdminCookie";
        private readonly static string _domain = ConfigurationManager.AppSettings["domain"];
        /// <summary>
        /// 设置管理员的信息
        /// </summary>
        /// <param name="adminId">管理员Id</param>
        /// <param name="adminUserName">管理员的用户名</param>
        /// <param name="adminNickName">管理员的昵称</param>
        /// <param name="adminLastLoginTime">上次登录时间</param>
        /// <param name="adminLastLoginIP">上次登录IP</param>
        /// <param name="adminLastLoginAddress">上次登录地址</param>
        /// <param name="authoryId">管理员权限Id</param>
        public static void SetCookie(string adminId, string adminUserName, string adminNickName, string adminLastLoginTime, string adminLastLoginIP, string adminLastLoginAddress,string authoryId)
        {
            string cookieValue = string.Join("|*&^%$#@!", adminId, HttpUtility.UrlEncode(adminUserName, System.Text.Encoding.UTF8), HttpUtility.UrlEncode(adminNickName, System.Text.Encoding.UTF8), adminLastLoginTime, adminLastLoginIP, HttpUtility.UrlEncode(adminLastLoginAddress, System.Text.Encoding.UTF8), authoryId);
            Sundry.Common.CookieHelper.SetCookie(adminCookieName, cookieValue,_domain);
        }
        /// <summary>
        /// 获取管理员基本信息
        /// </summary>
        /// <param name="index">【0：adminId，1：adminUserName，2：adminNickName，3：adminLastLoginTime，4：adminLastLoginIP，5：adminLastLoginAddress,6:管理员权限Id】</param>
        /// <returns></returns>
        public static string GetAdminInfo(int index)
        {
            string value = string.Empty;
            string cookieValue = Sundry.Common.CookieHelper.GetCookie(adminCookieName);
            if (!string.IsNullOrEmpty(cookieValue))
            {
                string[] adminInfo = cookieValue.Split(new string[] { "|*&^%$#@!" },StringSplitOptions.None);
                if (adminInfo.Length >= index)
                {
                    if (index == 1 || index == 2 || index == 5)
                    {
                        value = HttpUtility.UrlDecode(adminInfo[index], System.Text.Encoding.UTF8);
                    }
                    else
                    {
                        value = adminInfo[index];
                    }
                }
            }
            return value;
        }
        /// <summary>
        /// 判断管理员是否登陆
        /// </summary>
        /// <returns></returns>
        public static bool AdminCheckLogin()
        {
            if (Sundry.Common.CookieHelper.ExistCookie(adminCookieName))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 注销管理员Cookie
        /// </summary>
        public static void AdminLoginOut()
        {
            if (Sundry.Common.CookieHelper.ExistCookie(adminCookieName))
            {
                Sundry.Common.CookieHelper.ExpireCookie(adminCookieName);
            }
        }
        /// <summary>
        /// 获取当前管理员Id
        /// </summary>
        /// <returns></returns>
        public static string GetAdminId()
        {
            return GetAdminInfo(0) == "" ? "0" : GetAdminInfo(0);
        }
        /// <summary>
        /// 获取当前管理员权限Id
        /// </summary>
        /// <returns></returns>
        public static string GetAdminAuthoryId()
        {
            return GetAdminInfo(6) == "" ? "0" : GetAdminInfo(6);
        }

        #endregion
    }
}